
layui.use('laydate',function(){
	
	  var laydate = layui.laydate;
	   
	  var startTime =   laydate.render({
	      elem: '#timaStart',
	      done: function(value, date){
	    	  endTime.config.min={    	    		
	      	    	year:date.year,
	      	    	month:date.month-1,//关键
	                  date:date.date,
	                  hours:date.hours,
	                  minutes:date.minutes,
	                  seconds:date.seconds
	      	    };
	      }
	    });
	    
	 var endTime =  laydate.render({
  	        elem: '#timeEnd',
  	        done: function(value, date){
  	        	startTime.config.max={
  	    	    		year:date.year,
  	        	    	month:date.month-1,//关键
  	                    date:date.date,
  	                    hours:date.hours,
  	                    minutes:date.minutes,
  	                    seconds:date.seconds
  	    	    }
  	        }
  	    });
		 
	});
    
 
layui.use('table', function(){
  var table = layui.table;
 
  //监听表格复选框选择
  table.on('checkbox(demo)', function(obj){
    console.log(obj)
  });
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    //删除
    if(obj.event === 'del'){
      layer.confirm('真的删除行么', function(index){
    	layer.close(index);
        var url="../../listenPurchase/delete.action";
        var data={"isva":2,"purchaseId":da.purchaseId};
        $.post(url,data,function(mes){
        	if(mes>0){
        		layer.msg("删除成功");
        		setTimeout(() => {
        			window.location.reload();
				}, 500);
        		 
        	}
        },"json");
       
      });
     
    //修改
    } else if(obj.event === 'edit'){
    	layer.open({
  		  type: 2, //设置为iframe
  		  area: ['70%', '65%'],
  		  content: ['purchaseUpdate.html?id='+da.purchaseId, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
  		  ,success: function(layero, index){
  			var url="../../listenPurchase/selectPurchaseId.action";
  	    	var data={"purchaseId":da.purchaseId};
  	    	$.post(url,data,function(mes){
  	    		var body = layer.getChildFrame('body', index);
				var iframe = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
  	    		iframe.$("#pursId").val(mes.purchaseId);
				
				iframe.$("#remark").val(mes.purchaseExplain);
				// alert(mes.newPath);
				//调用子页面渲染的方法 刷新
				/*iframe.load();*/
  	    	},"json");
  		  }
  	  }); 
    //查看订单详情
    } else if(obj.event === 'chaDetail'){
    	layer.open({
    	  title: "采购订单详情",
  		  type: 2, //设置为iframe
  		  shift:1,//弹出框动画效果
  		  area: ['67.6%', '55%'],
  		  content: ['../purchaseDetail/purchaseDetail.html?id='+da.purchaseId, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
  		 
  	  }); 
    //查看审核详情
    }else if(obj.event === 'chaAudit'){
    	var url="../../listenPurchase/findSellId.action";
    	var data={"sort":da.purchaseId};
    	var sellIds;
    	$.post(url,data,function(mes){
    		$.each(mes,function(i,item){
    			sellIds=item.sellId;
    		})
    		layer.open({
    	      	  title: "采购审核详情",
    	    		  type: 2, //设置为iframe
    	    		  shift:1,//弹出框动画效果
    	    		  area: ['76.4%', '65%'],
    	    		  content: ['../purchaseMusicAudit/purchaseMusicAuditDetail.html?sellId='+sellIds+"&purchaseId="+da.purchaseId, 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    	    		 
    	    	  });
    	})
    	 
      //提交质检
      }else if(obj.event === 'quality'){
    	  var index = layer.load(0, {
  			  shade: [0.1,'#fff'] //0.1透明度的白色背景
  		});
    	  layer.confirm('是否提交质检', function(index){
    	    	layer.close(index);
    	        var url="../../listenPurchase/addQuality.action";
    	        var inputs=da.purchaseId+","+da.companyId+","+da.purchaseNumber;
    	        var data={"purchaserString":inputs}
    	        $.post(url,data,function(mes){
    	        	if(mes.datas==1){
    	        		var urls="../../listenPurchase/updatePurchaseState.action";
    					var dates ={"auditingStatus":5,"purchaseId":da.purchaseId};
    					$.post(urls,dates,function(mess){
    						if(mess.datas==0){
    							layer.msg("提交成功");
    							setTimeout(() => {
    							  window.location.reload()
  		    					  var index=parent.layer.getFrameIndex(window.name);
  		    					  parent.layer.close(index);
								}, 500);
								  
    						}
    					},"json")
    	        	}
    	        },"json");
    	      
    	      });
    	  //提交审核
        }else if(obj.event === 'subAudit'){
      	  layer.confirm('是否提交审核', function(index){
  	    	layer.close(index);
    		var urls = "../../listenPurchase/updatePurchaseState.action";
			var dates ={"auditingStatus":1,"purchaseId":da.purchaseId};
			$.post(urls,dates,function(mes){
				if(mes.datas == 0){
					layer.msg("提交成功");
					setTimeout(() => {
						  window.location.reload()
    					  var index=parent.layer.getFrameIndex(window.name);
    					  parent.layer.close(index);
					}, 500);
				}
			},"json")
  	      });
      }
  });
   
  //表单重载
  var $ = layui.$, active = {
		    reload: function(){
		      var demoReload = $('#demoReload');
		      var purDateStart=$('#timaStart');
		      var purDateEnd=$('#timeEnd');
		      var shzt=$('#shzt');
		      //执行重载
		      table.reload('idTest', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	companyName: demoReload.val(),
		            purDateStart: purDateStart.val(),
		            purDateEnd: purDateEnd.val(),
		            auditingStatus: shzt.val()
		        }
		      });
		    }
		  };
  
  $('.demoTable .layui-btn').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
 
  /**
  * 增加
  */
  $('#insert').on('click', function(){
	  layer.open({
		  type: 2, //设置为iframe
		  area: ['70%', '75%'],
		  content: ['purchaseAdd.html', 'on'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		  
	  }); 	
	          
  });
  
  
});